/****************************************/
/* Player.cpp		 					*/
/* Created on: Sep 4, 2011				*/
/* Copyright Playsoft 2011				*/
/* Released under GPL v2				*/
/****************************************/

#include "Player.h"
#include "Flower.h"
#include "Leg.h"

Player::Player(int p_xStartPos,int p_yStartPos):
	m_xPosition(p_xStartPos),
	m_yPosition(p_yStartPos),
	m_xSpeed(0),
	m_ySpeed(0),
	SpeeedModifier(1),
	jumpTimeout(0),
	lives(3)
{

	playerLeft= load_bmp("data/hero_st_l.bmp", default_palette );
	playerRight= load_bmp("data/hero_st_r.bmp", default_palette );
	playerJumpRight= load_bmp("data/hero_ju_r.bmp", default_palette );
	playerJumpLeft= load_bmp("data/hero_ju_l.bmp", default_palette );

	m_baseWalkSpeed=2.0f;
	m_jumpMaxSpeed=10.0f;
	m_jumpMaxTimeout=10.0f;
	m_jumpSpeed=0.0f;

	jump = load_sample("data/jump.wav");
	hit = load_sample("data/hit.wav");

	m_Landed=false;
	m_height=1000;
	m_landedOn=-1;
	m_PlayerOrientation=eOrientation_Right;
}
void Player::GoLeft()
{
	m_xSpeed-=m_baseWalkSpeed;

	m_PlayerOrientation=eOrientation_Left;

}
void Player::GoRight()
{
	m_xSpeed+=m_baseWalkSpeed;

	m_PlayerOrientation=eOrientation_Right;

}
void Player::Jump()
{
	if( (m_height<110.0f) && (m_height>-10.0f))//
	//if(m_Landed)
	{
		jumpTimeout=m_jumpMaxTimeout;

		m_Landed=false;

		play_sample(jump, 255, 128, 1000, FALSE);
	}
}

void Player::Update(float p_growSpeed,Flower*  p_flowers[6],Leg *legs[4])
{
	int l_elapsedTime=1;


	if (jumpTimeout>0)
	{

		jumpTimeout-=l_elapsedTime;
		m_jumpSpeed=m_jumpMaxSpeed;

	}
	else
	{
		jumpTimeout=0;
		if(!m_Landed)
		{
			m_jumpSpeed-=0.4f;
		}

		for(int i=0 ;i < 6;++i)
		{
			int height;
			int posX;
			int width;
			m_Landed=false;
			p_flowers[i]->GetPos(height,posX,width);
			height+=90;
			posX+=45;
			if( (m_xPosition<=posX+width)  && (m_xPosition+107>=posX) )
			{
 				m_height=height-m_yPosition;
				if( (m_yPosition+107.0f>=(float)height) && (m_yPosition+107.0f<=(float)height+20.0f)  )
				{
					m_yPosition=height-107.0f;
					m_jumpSpeed=0.0f;
					p_flowers[i]->PlayerOnFlower();
 					m_Landed=true;
					m_landedOn=i;

				}


			}

		}
		for(int i=0 ;i < 4;++i)
		{
			if(legs[i]!= 0x0 )
			{
				if( (m_xPosition+107>=legs[i]->locationX ) && (m_xPosition<=legs[i]->locationX + 139))
				{
					if(legs[i]->locationY + 126.0f >= m_yPosition)
					{
						lives--;
						legs[i]->hit();
						play_sample(hit, 255, 128, 1000, FALSE);
					}
				}
			}
		}

	}
	m_yPosition-=m_jumpSpeed;
	m_yPosition+=p_growSpeed;
	m_xPosition+=m_xSpeed;
	m_yPosition+=m_ySpeed;
	m_xSpeed*=0.5;
}

void Player::Draw(BITMAP *buffer)
{

	BITMAP *playerStance=NULL;

	if((m_PlayerOrientation == eOrientation_Left) && (!m_Landed))
	{
		playerStance=playerJumpLeft;
	}
	else if((m_PlayerOrientation == eOrientation_Right)&& (!m_Landed))
	{
		playerStance=playerJumpRight;
	}
	else if((m_PlayerOrientation == eOrientation_Left)&& (m_Landed))
	{
		playerStance=playerLeft;
	}
	else if((m_PlayerOrientation == eOrientation_Right)&& (m_Landed))
	{
		playerStance=playerRight;
	}
	if (playerStance!=NULL)
	{
		masked_blit(playerStance, buffer, 0, 0, (int)m_xPosition, (int)m_yPosition, 91, 107);
	}
}
